Page({
  data: {
    schema:[
    ],
    formData:{
    },
    title: '',
    result: '',
    remark: '',
    apiName: '',
    deviceList: [],
    loading: false,
    errorMsg: '',
    deviceId: '', // 设备ID
    deviceName: '', // 设备名称
    callStatus: 'idle', // 通话状态：idle(空闲)/connecting(连接中)/talking(通话中)/failed(失败)
    errorMsg: '' // 错误信息
  },
 
  getDeviceVoIPList(e) {
    this.setData({
      loading: true,
      errorMsg: ''
    });
    // 调用微信接口获取设备列表
    wx.getDeviceVoIPList({
      success: (res) => {
        console.log('获取VoIP设备列表成功', res);
        this.setData({
          deviceList: res.deviceList || [],
          loading: false,
          result: res,
          apiName: e.currentTarget.dataset.apiname 
        });
      },
      fail: (err) => {
        console.error('获取VoIP设备列表失败', err);
        this.setData({
          errorMsg: `获取失败: ${err.errMsg || '未知错误'}`,
          loading: false,
          result: err,
          apiName: e.currentTarget.dataset.apiname 
        });
      }
    });
  },
  requestDeviceVoIP(e) {
    if (!this.data.deviceId) {
      wx.showToast({
        title: '设备ID不能为空',
        icon: 'none'
      });
      return;
    }

    this.setData({
      callStatus: 'connecting',
      errorMsg: ''
    });

    // 调用请求设备VoIP通话接口
    wx.requestDeviceVoIP({
      sn: 'xxxx',
      snTicket: 'xxxxx',
      modelId: this.data.deviceId,
      deviceName: 'xxx',
      success: (res) => {
        console.log('VoIP请求成功', res);
        this.setData({
          callStatus: 'talking'
        });
        wx.showToast({
          title: '通话已建立',
          icon: 'success'
        });
      },
      fail: (err) => {
        console.error('VoIP请求失败', err);
        this.setData({
          callStatus: 'failed',
          errorMsg: err.errMsg || '请求通话失败，请重试'
        });
        wx.showToast({
          title: this.data.errorMsg,
          icon: 'none'
        });
      },
      complete: () => {
        // 无论成功失败都会执行
      }
    });
  },
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: options.title,
    })
    
    this.setData({
      title: options.title,
      zh:options.zh,
      remark: options.remark,
      webUrl: options.webUrl
    })
  }
})
